#!/usr/bin/env bash
#  SPDX-License-Identifier: BSD-3-Clause
#  Copyright (C) 2017 Intel Corporation
#  All rights reserved.
#
testdir=$(readlink -f "$(dirname "$0")")
rootdir=$(readlink -f "$testdir/../..")

source "$rootdir/test/common/autotest_common.sh"
source "$rootdir/test/dd/common.sh"

function cleanup() {
	rm -rf "$testdir/btest.out"
	rm -rf "$testdir/*.blob"
	rm -rf "$testdir/test.pattern"
}

# Create malloc bdev with 128MiB in size and 512B block size
declare -A method_bdev_malloc_create_0=(
	["name"]=Malloc0
	["num_blocks"]=262144
	["block_size"]=512
)

trap 'cleanup; exit 1' SIGINT SIGTERM EXIT

# Create test data file (1MiB random data)
dd if=/dev/urandom of="$testdir/test.pattern" bs=1M count=1

(cd "$testdir" \
	&& "$_examples_dir/blobcli" -j <(gen_conf) -b Malloc0 -T "$testdir/test.bs" > "$testdir/btest.out")

# The test script will import the test pattern generated by dd and then export
# it to a file so we can compare and confirm basic read and write
"$rootdir/test/app/match/match" -v "$testdir/btest.out.match"
diff "$testdir/test.pattern" "$testdir/test.pattern.blob"

trap - SIGINT SIGTERM EXIT
cleanup
